<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="书名"
        style="width: 200px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-input
        v-model="listQuery.author"
        placeholder="作者"
        style="width: 200px; margin-left: 5px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-select
        v-model="listQuery.category"
        placeholder="分类"
        style="margin-left: 5px"
        clearable
        class="filter-item"
        @change="handleFilter"
      >
        <!-- <el-option :value="1">11</el-option>
        <el-option :value="2">12</el-option>
        <el-option :value="3">13</el-option> -->
        <el-option
          v-for="item in categoryList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        查询
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        style="margin-left: 5px"
        @click="handleCreate"
      >
        新增
      </el-button>
      <el-checkbox
        v-model="showCover"
        class="filter-item"
        style="margin-left: 5px"
        @change="changeShowCover"
      >
        显示封面
      </el-checkbox>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="sortChange"
    >
      <el-table-column
        label="Id"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      />
      <el-table-column
        label="书名"
        align="center"
        width="150"
      >
        <template v-slot="{ row: {titleWrapper} }">
          <span v-html="titleWrapper" />
        </template>
      </el-table-column>
      <el-table-column
        label="作者"
        align="center"
        width="150"
      >
        <template v-slot="{ row: {authorWrapper} }">
          <span v-html="authorWrapper" />
        </template>
      </el-table-column>
      <el-table-column label="出版社" prop="introduction" align="center" width="100" />
      <el-table-column label="分类" prop="created_at" align="center" width="100" />
      <el-table-column label="语言" prop="updated_at" align="center" width="100" />
      <el-table-column
        v-if="showCover"
        label="封面"
        align="center"
        width="150"
      >
        <template slot-scope="scope">
          <a :href="scope.row.logo" target="_blank">
            <img :src="scope.row.logo" style="width:120px" />
          </a>
        </template>
      </el-table-column>
      <el-table-column label="文件名" prop="price" align="center" width="100" />
      <el-table-column label="文件路径" prop="price" align="center" width="100" />
      <el-table-column label="封面路径" prop="price" align="center" width="100" />
      <el-table-column label="解压路径" prop="price" align="center" width="100" />
      <el-table-column label="上传人" prop="created_at" align="center" width="100" />
      <el-table-column label="上传时间" prop="updated_at" align="center" width="100" />
      <el-table-column label="文件名" prop="price" align="center" width="100" />
      <el-table-column label="文件路径" prop="price" align="center" width="100" />
      <el-table-column label="封面路径" prop="price" align="center" width="100" />
      <el-table-column label="解压路径" prop="price" align="center" width="100" />
      <el-table-column label="上传人" prop="created_at" align="center" width="100" />
      <el-table-column
        label="操作"
        align="center"
        width="120"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '../../components/Pagination/index'
import waves from '../../directive/waves/waves'
import { getCategory, listBook } from '../../api/book'

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listQuery: {},
      showCover: false,
      categoryList: [],
      tableKey: 0,
      listLoading: true,
      list: [],
      total: 0
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getCategoryList()
    this.getList()
  },
  methods: {
    parseQuery() {
      const listQuery = {
        page: 1,
        pageSize: 20,
        sort: 1
      }
      this.listQuery = { ...listQuery, ...this.listQuery }
    },
    handleFilter() {
      console.log('handleFilter', this.listQuery)
      this.getList()
    },
    handleCreate() {
      this.$router.push('/book/create')
    },
    changeShowCover(value) {
      this.showCover = value
    },
    getCategoryList() {
      getCategory().then(response => {
        this.categoryList = response.data
      })
    },
    sortChange(data) {
      console.log('sortChange', data)
      const { order } = data
      this.sortBy(order)
    },
    sortBy(order) {
      if (order === 'ascending') {
        this.listQuery.sort = 1
      } else {
        this.listQuery.sort = 2
      }
      this.handleFilter()
    },
    wrapperKeyword(k, v) {
      function highlight(value) {
        return `<span style="color: #1890ff">${value}</span>`
      }
      if (!this.listQuery[k]) {
        return v
      } else {
        return v.replace(new RegExp(this.listQuery[k], 'ig'), v => highlight(v))
      }
    },
    getList() {
      this.listLoading = true
      listBook(this.listQuery).then(response => {
        const { list, total_items } = response.data
        this.list = list
        this.total = total_items
        this.listLoading = false
        this.list.forEach(book => {
          book.titleWrapper = this.wrapperKeyword('title', book.name)
          book.authorWrapper = this.wrapperKeyword('author', book.author)
        })
      })
    },
    handleUpdate(row) {
      console.log('handleUpdate', row.name)
      this.$router.push(`/book/edit/${row.name}`)
    }
  }
}
</script>
<style lang="scss" scoped></style>
